package com.sq.partner.manager.dao.impl;

import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.dao.LifeDataExportDao;
import com.sq.partner.manager.mybatis.model.LifeDataExportVO;
import com.sq.partner.manager.util.Page;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 2022/8/26 10:01
 */
@Repository
public class LifeDataExportDaoImpl extends BaseDaoImpl implements LifeDataExportDao {
	@Override
	public List<Map<String, Object>> queryLifeDataByCriteria(Page page) throws Exception {
		Integer count = 0;
		Map<String,Object> map = (Map<String, Object>) page.getMap();
		if(1 == (Integer) map.get("selectType")){
			count =(Integer) this.query("querySigningTimeCount", map);
		}
		if(2 == (Integer) map.get("selectType")){
			count =(Integer) this.query("queryPaidInTimeCount", map);
		}
		if(3 == (Integer) map.get("selectType")){
			count =(Integer) this.query("queryIncreaseTimeCount", map);
		}
		if(4 == (Integer) map.get("selectType")){
			count =(Integer) this.query("querySurrenderCount", map);
		}
		if(5 == (Integer) map.get("selectType")){
			count =(Integer) this.query("queryMagicProdCount", map);
		}
		page.setTotal(count);
		Boolean isExc = (Boolean) (map.get("isExc") == null ? false : map.get("isExc"));

		if (isExc){
			if (count>65535){
				throw new AppException("最多只能导出65535条数据,请缩小查询期间");
			}
			map.put("ENDROW",count);
		}
		//查询结果
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (1 == (Integer) map.get("selectType")){
			list = this.queryList("queryLifeDataByCriteria1", page.getMap());
		}else if (2 == (Integer) map.get("selectType")){
			list = this.queryList("queryLifeDataByCriteria2", page.getMap());
		}else if (3 == (Integer) map.get("selectType")){
			list = this.queryList("queryLifeDataByCriteria3", page.getMap());
		}else if (4 == (Integer) map.get("selectType")){
			list = this.queryList("queryLifeDataByCriteria4", page.getMap());
		}else if (5 == (Integer) map.get("selectType")){
			list = this.queryList("queryLifeDataByCriteria5", page.getMap());
		}
		return list;
	}

	@Override
	public Page queryLifePolicyAll(Page page) throws Exception {
		Integer count = 0;
		Map<String,Object> map = (Map<String, Object>) page.getMap();
		if(1 == (Integer) map.get("selectType")){
			count =(Integer) this.query("querySigningTimeCount", map);
		}
		if(2 == (Integer) map.get("selectType")){
			count =(Integer) this.query("queryPaidInTimeCount", map);
		}
		if(3 == (Integer) map.get("selectType")){
			count =(Integer) this.query("queryIncreaseTimeCount", map);
		}
		if(4 == (Integer) map.get("selectType")){
			count =(Integer) this.query("querySurrenderCount", map);
		}
		if(5 == (Integer) map.get("selectType")){
			count =(Integer) this.query("queryMagicProdCount", map);
		}
		page.setTotal(count);
		return page;
	}
}
